<template>
  <div>
    <div class="page-breadcrumb-wrap">
      <el-card class="page-breadcrumb">
        <span class="breadcrumb-button" @click="goBack">施工方案</span>
        <span class="breadcrumb-line">/</span>
        <span class="breadcrumb-active">方案详情</span>
      </el-card>
    </div>
    <div class="detail-box">
      <el-card>
        <el-form
          ref="form"
          :style="{ height: formHeight + 'px', 'overflow-y': 'auto', 'overflow-x': 'hidden' }"
          :model="rowData"
          label-width="120px"
        >
          <div class="form-header">
            <span class="form-title">审批详情</span>
            <el-button type="primary" @click="goBack()" plain>返 回</el-button>
          </div>
          <el-row>
            <el-col :span="8" class="mt10">
              <el-form-item label="项目:">
                {{ rowData.lineName }}
              </el-form-item>
            </el-col>
            <el-col :span="8" class="mt10">
              <el-form-item label="分部:">
                {{ rowData.lotName }}
              </el-form-item>
            </el-col>
            <el-col :span="8" class="mt10">
              <el-form-item label="工区:">
                {{ rowData.workArea }}
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="审批名称:">
                {{ rowData.projectName }}
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="权属单位:">
                {{ rowData.ownershipUnit }}
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="权属单位审批人:">
                {{ rowData.linkName }}
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="联系方式:">
                {{ rowData.linkPhone }}
              </el-form-item>
            </el-col>
          </el-row>
          <div class="form-header mt10">
            <span class="form-title">关联审批流程</span>
          </div>
          <el-row>
            <el-col :span="8" class="mt10">
              <el-form-item label="模型编码:">
                {{ rowData.moduleCode }}
              </el-form-item>
            </el-col>
            <el-col :span="8" class="mt10">
              <el-form-item label="模型名称:">
                {{ rowData.moduleName }}
              </el-form-item>
            </el-col>
            <el-col :span="8" class="mt10">
              <el-form-item label="生效版本:">
                {{ rowData.version }}
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="流程描述:">
                {{ rowData.remark }}
              </el-form-item>
            </el-col>
          </el-row>
          <div class="form-header mt10" style="border-bottom: none">
            <span class="form-title">设计方案</span>
            <span
            ><el-button
              icon="el-icon-time"
              title="历史版本"
              circle
              @click="viewHistoryVersion"
              v-if="rowData.schemeId"
            ></el-button
            ></span>
          </div>
          <div class="center-card">
            <div class="card-left">
              <span class="form-sub-title">涉敏区域</span>
              <div class="card-left-body">
                <el-col :span="24">
                  <el-form-item label="BIM模型:" label-width="120px">
                    {{ rowData.sensitiveAreaBim }}
                  </el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label="GIS边界文件:" label-width="120px">
                    <div style="height: 100px; overflow: auto; overflow-x: hidden">
                      <div class="file-item" v-for="item in rowData.gisFiles" :key="item.fileId">
                        <img src="../../../../assets/icon/antFill-file-excel.png" class="file-item-icon" alt="" />
                        <div class="file-item-content">
                          <div class="file-item-text" :title="item.fileName">{{ item.fileName }}</div>
                          <div class="file-item-button">
                            <el-button
                              type="text"
                              @click="download(item)"
                            ><i class="el-icon-download"></i>下载</el-button
                            >
                            <el-button type="text" @click="filePreview(item)">预览</el-button>
                          </div>
                        </div>
                      </div>
                    </div>
                  </el-form-item>
                </el-col>
              </div>
            </div>
            <div class="card-line"></div>
            <div class="card-center">
              <span class="form-sub-title">施工设计</span>
              <div class="card-center-body">
                <el-col :span="24">
                  <el-form-item label="BIM模型:" label-width="120px">
                    {{ rowData.constructDesignBim }}
                  </el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label="施工方案文件:" label-width="120px">
                    <div style="height: 100px; overflow: auto; overflow-x: hidden">
                      <div class="file-item" v-for="item in rowData.schemeFiles" :key="item.fileId">
                        <img src="../../../../assets/icon/antFill-file-excel.png" class="file-item-icon" alt="" />
                        <div class="file-item-content">
                          <div class="file-item-text" :title="item.fileName">{{ item.fileName }}</div>
                          <div class="file-item-button">
                            <el-button
                              type="text"
                              @click="download(item)"
                            ><i class="el-icon-download"></i>下载</el-button
                            >
                            <el-button type="text" @click="filePreview(item)">预览</el-button>
                          </div>
                        </div>
                      </div>
                    </div>
                  </el-form-item>
                </el-col>
              </div>
            </div>
          </div>
          <div v-if="rowData.processInstanceId">
            <div class="form-header" style="margin-top: 40px">
              <span class="form-title">审批进度</span>
              <el-button type="text" @click="viewFlowPicture">查看流程图</el-button>
            </div>
            <div style="padding: 10px">
              <el-steps :active="active" align-center>
                <el-step
                  v-for="(item, index) in flowList"
                  :title="item.taskName + ' ' + item.userName"
                  :key="index"
                  :icon="!item.endTime ? 'el-icon-info' : 'el-icon-circle-check'"
                >
                  <template #description>
                    <div>
                      <div style="color: #303133">{{ item.endTime }}</div>
                      <el-popover placement="top" trigger="click">
                        <div class="file-item" v-for="ele in item.attachments" :key="ele.fileId">
                          <img src="../../../../assets/icon/antFill-file-excel.png" class="file-item-icon" alt="" />
                          <div class="file-item-content">
                            <div class="file-item-text" :title="ele.originalFileName">{{ ele.originalFileName }}</div>
                            <div class="file-item-button">
                              <el-button type="text" @click="download(ele)">下载</el-button>
                              <el-button type="text" @click="filePreview(ele)">预览</el-button>
                            </div>
                          </div>
                        </div>
                        <el-button slot="reference" v-if="item.attachments.length > 0" type="text">查看</el-button>
                      </el-popover>
                    </div>
                    <div :title="item.comment" style="color: red;width:100%;overflow: hidden;white-space: nowrap;text-overflow: ellipsis;">{{ item.comment }}</div>
                  </template>
                </el-step>
              </el-steps>
            </div>
          </div>
          <div v-if="rowData.status === 30">
            <div class="form-header">
              <span class="form-title">审批情况 </span>
              <el-button
                style="margin-left: 20px"
                @click="editApprovalStatus"
                v-if="menuButtonPermissions.includes('gz-metro-server.constructionScheme.updApprovalInfo')"
              ><i class="el-icon-edit"></i>编辑</el-button
              >
            </div>
            <el-row>
              <el-col :span="8" class="mt10">
                <el-form-item label="权属/管理单位:" label-width="200px">
                  {{ rowData.ownershipUnit }}
                </el-form-item>
              </el-col>
              <el-col :span="8" class="mt10">
                <el-form-item label="鉴定情况:" label-width="200px">
                  {{ rowData.identify }}
                </el-form-item>
              </el-col>
              <el-col :span="8" class="mt10">
                <el-form-item label="风险等级:" label-width="200px">
                  {{ rowData.riskLevel }}
                </el-form-item>
              </el-col>
              <el-col :span="8" class="mt10">
                <el-form-item label="风险管控等级:" label-width="200px">
                  {{ rowData.riskControlLevel }}
                </el-form-item>
              </el-col>
              <el-col :span="8" class="mt10">
                <el-form-item label="风险管控措施:" label-width="200px">
                  {{ rowData.riskControlMeasures }}
                </el-form-item>
              </el-col>
              <el-col :span="8" class="mt10">
                <el-form-item label="施工单位:" label-width="200px">
                  {{ rowData.constructionUnit }}
                </el-form-item>
              </el-col>
              <el-col :span="8" class="mt10">
                <el-form-item label="项目经理:" label-width="200px">
                  {{ rowData.projectManager }}
                </el-form-item>
              </el-col>
              <el-col :span="8" class="mt10">
                <el-form-item label="项目经理联系方式:" label-width="200px">
                  {{ rowData.projectPhone }}
                </el-form-item>
              </el-col>
              <el-col :span="8" class="mt10">
                <el-form-item label="监理单位名称:" label-width="200px">
                  {{ rowData.supervisionUnit }}
                </el-form-item>
              </el-col>
              <el-col :span="8" class="mt10">
                <el-form-item label="监理单位项目总监:" label-width="200px">
                  {{ rowData.directorName }}
                </el-form-item>
              </el-col>
              <el-col :span="8" class="mt10">
                <el-form-item label="监理单位项目总监联系方式:" label-width="200px">
                  {{ rowData.directorPhone }}
                </el-form-item>
              </el-col>
              <el-col :span="8" class="mt10">
                <el-form-item label="预计穿越开始时间:" label-width="200px">
                  {{ rowData.throughStartTime }}
                </el-form-item>
              </el-col>
              <el-col :span="8" class="mt10">
                <el-form-item label="预计穿越结束时间:" label-width="200px">
                  {{ rowData.throughEndTime }}
                </el-form-item>
              </el-col>
              <el-col :span="8" class="mt10">
                <el-form-item label="应急预案状态:" label-width="200px">
                  {{ rowData.emergencyPlatType === 0 ? '已制定' : (rowData.emergencyPlatType === 1 ? '未定制' : '不需要') }}
                </el-form-item>
              </el-col>
              <el-col v-if="rowData.emergencyPlatType !== 2" :span="8" class="mt10">
                <el-form-item :label="rowData.protocolType===0 ? '应急预案完成时间:' : '预计完成时间:'" label-width="200px">
                  {{ rowData.emergencyPlatTime }}
                </el-form-item>
              </el-col>
              <el-col v-if="rowData.emergencyPlatType !== 0" :span="8" class="mt10">
                <el-form-item :label="rowData.emergencyPlatType === 2 ? '不需要的原因:':'应急预案未制定的原因:'" label-width="200px">
                  {{ rowData.emergencyPlatReason }}
                </el-form-item>
              </el-col>
              <el-col :span="8" class="mt10">
                <el-form-item label="安全评估和专家评审状态:" label-width="200px">
                  {{ rowData.safetyType === 0 ? '已制定' : (rowData.safetyType === 1 ? '未制定' : '不需要') }}

                </el-form-item>
              </el-col>
              <el-col v-if="rowData.safetyType !== 2" :span="8" class="mt10">
                <el-form-item :label="rowData.safetyType===0 ? '安全评估和专家评审完成时间:' : '预计完成时间:'" label-width="200px">
                  {{ rowData.safetyTime }}
                </el-form-item>
              </el-col>
              <el-col v-if="rowData.safetyType !== 0" :span="8" class="mt10">
                <el-form-item :label="rowData.safetyType === 2 ? '不需要的原因:':'安全评估和专家评审未制定的原因:'" label-width="200px">
                  {{ rowData.safetyReason }}
                </el-form-item>
              </el-col>
              <el-col :span="8" class="mt10">
                <el-form-item label="施工作业安全保护协议状态:" label-width="200px">
                  {{ rowData.protocolType === 0 ? '已签订' : (rowData.protocolType === 1 ? '未签订' : '不需要') }}
                </el-form-item>
              </el-col>
              <el-col v-if="rowData.protocolType !== 2" :span="8" class="mt10">
                <el-form-item :label="rowData.protocolType===0 ? '施工作业安全保护协议完成时间:' : '预计预计完成时间完成时间:'" label-width="200px">
                  {{ rowData.protocolTime }}
                </el-form-item>
              </el-col>
              <el-col v-if="rowData.protocolType !== 0" :span="8" class="mt10">
                <el-form-item :label="rowData.protocolType === 2 ? '不需要的原因:':'签订施工作业安全保护协议状态未签订的原因:'" label-width="200px">
                  {{ rowData.protocolReason }}
                </el-form-item>
              </el-col>
            </el-row>
          </div>
        </el-form>
      </el-card>
    </div>
    <flow-picture ref="flowPictureRef" />
    <history-version ref="historyVersionRef" />
    <approval-status ref="approvalStatusRef" @goBack="goBack" />
  </div>
</template>
<script>
import actions from '@/cros/actions'
import { download, getDetail, getModuleById, getProcessHistory } from '@/api/constructionScheme'
import FlowPicture from '@/views/designingScheme/designingScheme/components/FlowPicture.vue'
import HistoryVersion from '@/views/constructionScheme/designingScheme/components/HistoryVersion.vue'
import ApprovalStatus from '@/views/constructionScheme/designingScheme/components/ApprovalStatus.vue'

export default {
  name: 'DetailPage',
  components: { ApprovalStatus, HistoryVersion, FlowPicture },
  data () {
    return {
      rowData: {},
      active: 0,
      flowList: [],
      menuButtonPermissions: [],
      formHeight: 500
    }
  },
  mounted () {
    actions.onGlobalStateChange((state, prevState) => {
      console.log(state, 'statestatestate')
      this.menuButtonPermissions = state.menuButtonPermissions.map((v) => v.buttonKey)
    }, true)
    // 监听窗口大小变化事件
    window.addEventListener('resize', this.setTableHeight)
    // 初始化表格高度
    this.setTableHeight()
  },
  methods: {
    filePreview (item) {
      this.$filePreview(item)
    },
    setTableHeight () {
      this.$nextTick(() => {
        this.formHeight = document.body.offsetHeight - 160
      })
    },
    init (row) {
      console.log(row, '111')
      this.rowData = JSON.parse(JSON.stringify(row))
      if (row.schemeId) {
        getDetail({ schemeId: row.schemeId }).then((res) => {
          console.log(res)
          if (res.code === 'success' && res.data) {
            this.$set(this.rowData, 'moduleId', res.data.moduleId)
            this.$set(this.rowData, 'sensitiveAreaBim', res.data.sensitiveAreaBim)
            this.$set(this.rowData, 'gisFiles', res.data.gisFiles)
            this.$set(this.rowData, 'constructDesignBim', res.data.constructDesignBim)
            this.$set(this.rowData, 'preFinishTime', res.data.preFinishTime)
            this.$set(this.rowData, 'sensitiveAreaList', res.data.sensitiveAreaList)
            this.$set(this.rowData, 'schemeFiles', res.data.schemeFiles)
            this.$set(this.rowData, 'moduleId', res.data.moduleId)
            this.$set(this.rowData, 'versionId', res.data.versionId)
            this.$set(this.rowData, 'processInstanceId', res.data.processInstanceId)
            this.$set(this.rowData, 'status', res.data.status)
            if (res.data) {
              this.rowData = {
                ...this.rowData,
                ...res.data
              }
            }
            getModuleById({
              modelId: this.rowData.moduleId,
              versionId: this.rowData.versionId
            }).then((res) => {
              if (res.code === 'success') {
                this.$set(this.rowData, 'moduleCode', res.data.moduleCode)
                this.$set(this.rowData, 'moduleName', res.data.moduleName)
                this.$set(this.rowData, 'version', res.data.version)
                this.$set(this.rowData, 'remark', res.data.remark)
              }
            })
            if (this.rowData.processInstanceId) {
              this.getProcessHistory()
            }
          }
        })
      } else {
        getModuleById({
              modelId: this.rowData.constructionModuleId,
              versionId: this.rowData.versionId
            }).then((res) => {
              if (res.code === 'success') {
                this.$set(this.rowData, 'moduleCode', res.data.moduleCode)
                this.$set(this.rowData, 'moduleName', res.data.moduleName)
                this.$set(this.rowData, 'version', res.data.version)
                this.$set(this.rowData, 'remark', res.data.remark)
              }
            })
      }
    },
    // 获取流程进度
    getProcessHistory () {
      getProcessHistory({ processId: this.rowData.processInstanceId }).then((res) => {
        if (res.code === 'success') {
          this.flowList = res.data
          this.flowList.forEach((item, index) => {
            if (item.endTime) {
              this.active = index + 1
              console.log(this.active, 'this.active')
            }
          })
          // if (this.flowList[this.flowList.length - 1].endTime) {
          //   this.active = this.flowList.length
          // }
        }
      })
    },
    // 查看历史版本
    viewHistoryVersion () {
      this.$nextTick(() => {
        this.$refs.historyVersionRef.init(this.rowData.schemeId)
      })
    },
    // 下载文件
    download (row) {
      download(row.fileId).then((res) => {
        console.log(res)
        const blob = new Blob([res.res])
        const url = window.URL.createObjectURL(blob)
        const link = document.createElement('a') // 创建a标签
        link.href = url
        link.download = row.originalFileName || row.fileName // 重命名文件
        link.click()
        URL.revokeObjectURL(url) // 释放内
      })
    },
    // 查看流程图
    viewFlowPicture () {
      this.$nextTick(() => {
        this.$refs.flowPictureRef.init(this.rowData.processInstanceId)
      })
    },
    // 编辑审批情况
    editApprovalStatus () {
      this.$nextTick(() => {
        this.$refs.approvalStatusRef.init(this.rowData)
      })
    },
    goBack () {
      this.$emit('changeShowType', 1)
    }
  }
}
</script>
<style scoped lang="scss">
.mt10 {
  margin-top: 10px;
}

.detail-box {
  padding: 0 10px;
}

.form-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px;
  border-bottom: 1px solid #ddd;
}

.form-title {
  color: #101010;
  font-size: 16px;
  font-weight: bold;
}

.form-sub-title {
  margin-bottom: 10px;
  color: #101010;
  font-size: 15px;
  font-weight: bold;
}

.center-card {
  display: flex;
  align-items: center;
  width: 100%;
  padding: 20px;
  border-radius: 4px;
  border: 1px solid #ebeef5;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.card-left {
  width: calc(49% - 21px);

  .card-left-body {
    padding: 10px 30px;
    height: 200px;
  }
}

.card-center {
  width: calc(49% - 21px);

  .card-center-body {
    padding: 10px 30px;
    height: 200px;
  }
}

.card-line {
  margin: 0 20px;
  height: 180px;
  width: 1px;
  background: #ddd;
}

.file-item {
  display: flex;
  margin-bottom: 10px;
}

.file-item-icon {
  width: 45px;
  height: 50px;
}

.file-item-content {
  margin-left: 10px;
  height: 50px;
   width: calc(100% - 45px);
  .file-item-text {
    height: 25px;
    font-weight: 700;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
  }

  .file-item-button {
    height: 25px;
  }
}

::v-deep .el-step__title.is-finish {
  font-weight: 700;
  color: #303133;
}
::v-deep .el-step__title.is-wait {
  font-weight: 700;
  color: #303133;
}
::v-deep .el-step__head.is-success {
  color: #409eff;
  border-color: #409eff;
}
::v-deep .el-icon-info {
  color: #eca600;
}

::v-deep .el-step__title {
  font-size: 14px;
}
::v-deep .el-step {
  margin-top: 20px;
  flex-basis: 20% !important;
  max-width: none !important;
}
::v-deep .el-steps {
  flex-wrap: wrap;
}
.backBox {
  margin: 20px 0;
  text-align: center;
}
</style>
